package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;

import com.snail.model.dto.user.RefreshTokenDTO;
import com.snail.model.dto.user.UserLoginDTO;
import com.snail.model.dto.user.UserRegisterDTO;
import com.snail.model.pojo.user.SysUser;
import com.snail.model.vo.user.LoginVO;
import com.snail.result.Result;
import com.snail.result.ResultCode;
import com.snail.service.CaptchaService;
import com.snail.service.EmailService;
import com.snail.service.SysUserService;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Map;
import java.util.UUID;

/**
 * 用户认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Validated
public class AuthController {
    
    private final SysUserService userService;
    private final CaptchaService captchaService;
    private final EmailService emailService;
    
    /**
     * 用户注册
     * @param registerDTO 注册请求参数
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<?> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        // 验证验证码
        boolean isCaptchaValid = captchaService.validateCaptcha(
            registerDTO.getCaptchaKey(),
            registerDTO.getCaptcha()
        );

        if (!isCaptchaValid) {
            return Result.failed(ResultCode.CLIENT_AUTHENTICATION_FAILED, "验证码错误");
        }

        // 创建用户对象
        SysUser user = new SysUser();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setEmail(registerDTO.getEmail());
        user.setPhone(registerDTO.getPhone());

        return userService.register(user);
    }
    
    /**
     * 用户登录
     * @param loginDTO 登录请求参数
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result login(@Valid @RequestBody UserLoginDTO loginDTO) {
        // 验证验证码
        boolean isCaptchaValid = captchaService.validateCaptcha(
            loginDTO.getCaptchaKey(),
            loginDTO.getCaptcha()
        );

        if (!isCaptchaValid) {
            return Result.failed(ResultCode.CLIENT_AUTHENTICATION_FAILED, "验证码错误");
        }

        return userService.login(loginDTO.getUsername(), loginDTO.getPassword());
    }

    /**
     * 发送邮箱验证码
     */
    @PostMapping("/sendResetEmail")
    public Result<String> sendResetEmail(@RequestParam String email) {
        emailService.sendResetEmail(email);
        return Result.success("验证码已发送");
    }

    /**
     * 重置密码 (通过邮箱验证码)
     */
    @PostMapping("/resetPasswordByEmail")
    public Result<String> resetPasswordByEmail(@RequestParam String email,
                                               @RequestParam String code,
                                               @RequestParam String newPassword) {
        if (!emailService.validateResetEmail(email, code)) {
            return Result.failed("验证码错误或已过期");
        }
        userService.resetPasswordByEmail(email, newPassword);
        return Result.success("密码已重置");
    }


    /**
     * 用户登出
     * @return 登出结果
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.success("登出成功");
    }
    
    /**
     * 获取验证码
     * @return 验证码信息，包含base64图片和验证标识
     */
    @GetMapping("/captcha")
    public Result<Map<String, String>> getCaptcha() {
        return captchaService.generateCaptcha();
    }

}